const router = require('koa-router')()
const { checkuser, add } = require('../service/user')
router.prefix('/users')

router.get('/', function (ctx, next) {
  ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})


router.post('/login', async function (ctx, next) {
  const body = ctx.request.body


  // if (body.username == 'admin'&&body.pwd=='123456')//硬编码
  const result = await checkuser(body.username, body.pwd)
  if (result)//在数据库里查找对应用户记录
  {
    delete result.pwd
    console.log(result)
    ctx.body = { errno: 0, data: result }  // status:OK,, user:{} userid
  }
   


  else ctx.body = { errno: 10001, message: '登录失败' }


  //result.errno ?=0  
})


router.post('/reg', async function (ctx, next) {
  const body = ctx.request.body


  // if (body.username == 'admin'&&body.pwd=='123456')//硬编码
  const result = await add(body)

  console.log(result)
  if (result)//在数据库里查找对应用户记录
    ctx.body = { errno: 0, message: '注册成功', result }  // status:OK


  else ctx.body = { errno: 10001, message: '注册失败' }


  //result.errno ?=0  
})

module.exports = router
